﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PortalLogReport.Models;
using PortalLogReport.Common;
using System.Data;
using Vertica.Data.VerticaClient;


namespace PortalLogReport.Controllers
{
    public class SettingsController : Controller
    {
        //
        // GET: /Settings/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Dimension()
        {
            string comdStr = "select L_1_ID, Dim_1, Dim_1_TXT, FRQCY from level_1";
            CommandType command = CommandType.Text;
            DataSet ds = VerticalHelper.ExecuteDataset(command, comdStr);
            List<Level1Model> lms = new List<Level1Model>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Level1Model lm = new Level1Model();
                lm.Id = dr[0].ToString();
                lm.Dim1 = dr[1].ToString();
                lm.Dim1_Text = dr[2].ToString();
                lm.FRQCY = dr[3].ToString();
                lms.Add(lm);
            }
            ViewBag.Level1Models = lms;
            return View();
        }


        public ActionResult DimensionEdit(string id)
        {
            string[] FRQCY = { "Year", "Month", "Week" };
            ViewBag.FRQCY = FRQCY;
            var Model = new Level1Model();
            if (Convert.ToInt32(id) != 0)
            {
                string comdStr = "select L_1_ID, Dim_1, Dim_1_TXT, FRQCY from level_1 where L_1_ID=" + id;
                CommandType command = CommandType.Text;
                VerticaDataReader dr = VerticalHelper.ExecuteReader(command, comdStr);
                while (dr.Read())
                {
                    Model.Id = dr["L_1_ID"].ToString();
                    Model.Dim1 = dr["Dim_1"].ToString();
                    Model.Dim1_Text = dr["Dim_1_TXT"].ToString();
                    Model.FRQCY = dr["FRQCY"].ToString();
                }
            }
            ViewBag.Model = Model;
            return View();
        }

        [HttpPost]
        public ActionResult Level1Save(Level1Model lm)
        {
            try
            {
                string comdStr = string.Empty;
                if (!string.IsNullOrEmpty(lm.Id) && lm.Id != "0")
                {
                    comdStr = "update level_1 set Dim_1='" + lm.Dim1 + "', Dim_1_TXT='" + lm.Dim1_Text + "', FRQCY='" + lm.FRQCY + "' where L_1_ID=" + lm.Id;

                }
                else
                {
                    comdStr = "insert into level_1 (Dim_1, Dim_1_TXT, FRQCY) values ('" + lm.Dim1 + "', '" + lm.Dim1_Text + "', '" + lm.FRQCY + "') ";
                }
                CommandType command = CommandType.Text;
                VerticalHelper.ExecuteNonQuery(command, comdStr);
                return Redirect("Dimension");
            }
            catch (Exception ex)
            {
                return Redirect("DimensionEdit\\" + lm.Id);
            }
        }

        public ActionResult Level2(string id)
        {
            if (!string.IsNullOrEmpty(id)) Session["l1id"] = id;
            else id = Session["l1id"].ToString();
            string comdStr = "select L_2_ID, Dim, Dim_TXT from level_2 where L_1_ID=" + id;
            CommandType command = CommandType.Text;
            DataSet ds = VerticalHelper.ExecuteDataset(command, comdStr);
            List<Level2Model> lms = new List<Level2Model>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Level2Model lm = new Level2Model();
                lm.Id = dr[0].ToString();
                lm.Dim = dr[1].ToString();
                lm.Dim_TXT = dr[2].ToString();
                lms.Add(lm);
            }
            ViewBag.Level2Models = lms;
            return View();
        }

        public ActionResult Level2Edit(string id)
        {
            var Model = new Level2Model();
            Model.Id = id;
            if (Convert.ToInt32(id) != 0)
            {
                string comdStr = "select L_2_ID, L_1_ID, Dim, Dim_TXT from level_2 where L_2_ID=" + id;
                CommandType command = CommandType.Text;
                VerticaDataReader dr = VerticalHelper.ExecuteReader(command, comdStr);
                while (dr.Read())
                {
                    Model.Id = dr["L_2_ID"].ToString();
                    Model.Dim = dr["Dim"].ToString();
                    Model.Dim_TXT = dr["Dim_TXT"].ToString();
                    Model.L_1_ID = dr["L_1_ID"].ToString();
                }
            }
            ViewBag.Model = Model;
            return View();
        }

        [HttpPost]
        public ActionResult Level2Save(Level2Model lm)
        {
            try
            {
                string comdStr = string.Empty;
                if (!string.IsNullOrEmpty(lm.Id) && lm.Id != "0")
                {
                    comdStr = "update level_2 set Dim='" + lm.Dim + "', Dim_TXT='" + lm.Dim_TXT + "' where L_2_ID=" + lm.Id;

                }
                else
                {
                    comdStr = "insert into level_2 (L_1_ID, Dim, Dim_TXT) values ('" + Session["l1id"] + "', '" + lm.Dim + "', '" + lm.Dim_TXT + "') ";
                }
                CommandType command = CommandType.Text;
                VerticalHelper.ExecuteNonQuery(command, comdStr);
                return Redirect("Level2");
            }
            catch (Exception ex)
            {
                return Redirect("Level2Edit\\" + lm.Id);
            }
        }

        public ActionResult Level3(string id)
        {
            if (!string.IsNullOrEmpty(id)) Session["l2id"] = id;
            else id = Session["l2id"].ToString();
            string comdStr = "select L_3_ID, Regex_NM, Regex_Format from level_3 where L_2_ID=" + id;
            CommandType command = CommandType.Text;
            DataSet ds = VerticalHelper.ExecuteDataset(command, comdStr);
            List<Level3Model> lms = new List<Level3Model>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Level3Model lm = new Level3Model();
                lm.Id = dr[0].ToString();
                lm.Regex_NM = dr[1].ToString();
                lm.Regex_Format = dr[2].ToString();
                lms.Add(lm);
            }
            ViewBag.Level3Models = lms;
            return View();
        }

        public ActionResult Level3Edit(string id)
        {
            var Model = new Level3Model();
            if (Convert.ToInt32(id) != 0)
            {
                string comdStr = "select L_3_ID, Regex_NM, Regex_Format from level_3 where L_3_ID=" + id;
                CommandType command = CommandType.Text;
                VerticaDataReader dr = VerticalHelper.ExecuteReader(command, comdStr);
                while (dr.Read())
                {
                    Model.Id = dr["L_3_ID"].ToString();
                    Model.Regex_NM = dr["Regex_NM"].ToString();
                    Model.Regex_Format = dr["Regex_Format"].ToString();
                }
            }
            ViewBag.Model = Model;
            return View();
        }

        [HttpPost]
        public ActionResult Level3Save(Level3Model lm)
        {
            try
            {
                string comdStr = string.Empty;
                if (!string.IsNullOrEmpty(lm.Id) && lm.Id != "0")
                {
                    comdStr = "update level_3 set Regex_NM='" + lm.Regex_NM + "', Regex_Format='" + lm.Regex_Format + "' where L_3_ID=" + lm.Id;

                }
                else
                {
                    comdStr = "insert into level_3 (L_2_ID, Regex_NM, Regex_Format) values ('" + Session["l2id"] + "', '" + lm.Regex_NM + "', '" + lm.Regex_Format + "') ";
                }
                CommandType command = CommandType.Text;
                VerticalHelper.ExecuteNonQuery(command, comdStr);
                return Redirect("Level3");
            }
            catch (Exception ex)
            {
                return Redirect("Level3Edit\\" + lm.Id);
            }
        }


        public ActionResult Level1Delete(string id)
        {
            string comdStr = string.Empty;
            if (!string.IsNullOrEmpty(id) && id != "0")
            {
                comdStr = "delete from level_1 where L_1_ID=" + id;
                CommandType command = CommandType.Text;
                VerticalHelper.ExecuteNonQuery(command, comdStr);
            }
            return Redirect("\\Settings\\Dimension");
        }

        public ActionResult Level2Delete(string id)
        {
            string comdStr = string.Empty;
            if (!string.IsNullOrEmpty(id) && id != "0")
            {
                comdStr = "delete from level_2 where L_2_ID=" + id;
                CommandType command = CommandType.Text;
                VerticalHelper.ExecuteNonQuery(command, comdStr);
            }
            return Redirect("\\Settings\\Level2");
        }

        public ActionResult Level3Delete(string id)
        {
            string comdStr = string.Empty;
            if (!string.IsNullOrEmpty(id) && id != "0")
            {
                comdStr = "delete from level_3 where L_3_ID=" + id;
                CommandType command = CommandType.Text;
                VerticalHelper.ExecuteNonQuery(command, comdStr);
            }
            return Redirect("\\Settings\\Level3");
        }
    }
}

